<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="group" class="menu" node-key="sortId" :data="group" :props="groupsProps" :current-node-key="''"
							 :highlight-current="true" :expand-on-click-node="false" :filter-node-method="groupFilterNode" @node-click="groupClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
								<span class="do">
									<el-button-group>
										<el-button icon="el-icon-edit" size="small" @click.stop="fileMapSortEdit(data)"></el-button>
										<el-button icon="el-icon-delete" size="small" @click.stop="fileMapSortDel(node, data)"></el-button>
									</el-button-group>
								</span>
							</span>
						</template>
					</el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" style="width: 100%;" @click="addSort">添加目录</el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
					<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="目录名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="目录名称" prop="titleName" width="150"></el-table-column>
					<el-table-column label="创建人" prop="createUserName" width="100"></el-table-column>
					<el-table-column label="创建时间" prop="createTime" width="150" sortable='custom'></el-table-column>
					<el-table-column label="备注说明" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="270">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="setOutRole(scope.row, scope.$index)">外网权限</el-button>
								<el-button text type="primary" size="small" @click="setInRole(scope.row, scope.$index)">内网权限</el-button>
								<el-button text type="primary" size="small" @click="edit_table(scope.row, scope.$index)">编辑</el-button>
								<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
									<template #reference>
										<el-button text type="primary" size="small">删除</el-button>
									</template>
								</el-popconfirm>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<sortSaveDialog v-if="dialog.sortSave" ref="sortSaveDialog" @success="handleSortSuccess" @closed="dialog.sortSave=false"></sortSaveDialog>
	<outRoleSave v-if="dialog.outSave" ref="saveOutDialog" @success="handleSuccess" @closed="dialog.outSave=false"></outRoleSave>
	<inRoleSave v-if="dialog.inSave" ref="saveInDialog" @success="handleSuccess" @closed="dialog.inSave=false"></inRoleSave>
</template>

<script>
import saveDialog from './save'
import sortSaveDialog from './sortSave'
import outRoleSave from './outRoleSave'
import inRoleSave from './inRoleSave'
export default {
	name: 'File Map Setting',
	components: {
		sortSaveDialog,
		saveDialog,
		outRoleSave,
		inRoleSave
	},
	data() {
		return {
			dialog: {
				save: false,
				sortSave:false,
				outSave:false,
				inSave:false
			},
			sortId:'',
			showGrouploading: false,
			groupFilterText: '',
			group: [],
			groupsProps: {
				label:"name",
				value: "sortId"
			},
			apiObj: this.$API.file.fileMap.getFileItemListForManage,
			selection: [],
			search: {
				keyword: null
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.group.filter(val);
		}
	},
	mounted() {
		this.getFileMapSortTree()
	},
	methods: {
		addSort()
		{
			this.dialog.sortSave = true
			this.$nextTick(() => {
				this.$refs.sortSaveDialog.open()
			})
		},
		//添加
		add(){
			if(this.sortId=='')
			{
				this.$alert("请选择左侧目录", "提示", {type: 'error'})
			}else
			{
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open().setData({sortNo:0,sortId:this.sortId})
				})
			}
		},
		edit_table(row,index)
		{
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open("edit").setData(row)
			})
		},
		//编辑
		setOutRole(row){
			this.dialog.outSave = true
			this.$nextTick(() => {
				this.$refs.saveOutDialog.open('edit').setData(row)
			})
		},
		setInRole(row){
			this.dialog.inSave = true
			this.$nextTick(() => {
				this.$refs.saveInDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row, index){
			const loading = this.$loading();
			var reqData = {itemId: row.itemId}
			var res = await this.$API.file.fileMap.deleteFileMapItem.post(reqData);
			if(res.code == 200){
				this.$refs.table.refresh();
				this.$message.success(res.message);
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
			loading.close();
		},
		//批量删除
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var itemIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					itemIdArr.push(this.selection[i].itemId);
				}
				const loading = this.$loading();
				var res = await this.$API.file.fileMap.deleteFileMapItemIds.post({ids: itemIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//加载树数据
		async getFileMapSortTree(){
			this.showGrouploading = true;
			var res = await this.$API.file.fileMap.getFileMapSortTree.get();
			this.showGrouploading = false;
			this.group = res.data;
		},
		//树过滤
		groupFilterNode(value, data){
			if (!value) return true;
			return data.name.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data){
			this.sortId = data.sortId;
			this.$refs.table.reload({sortId: data.sortId})
		},
		fileMapSortEdit(data) {
			this.dialog.sortSave = true
			this.$nextTick(() => {
				var editNode = this.$refs.group.getNode(data.sortId);
				var editNodeParentId = editNode.level == 1 ? '0' : editNode.parent.data.sortId;
				data.parentId = editNodeParentId
				this.$refs.sortSaveDialog.open("edit").setData(data)
			})

		},
		fileMapSortDel(node, data) {
			this.$confirm(`确定删除 ${data.name} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				var res = await this.$API.file.fileMap.deleteFileMapSort.post({sortId: data.sortId});
				if (res.code == 200) {
					this.$refs.group.remove(data.sortId)
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}

			}).catch(() => {

			})
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode){
			this.$refs.table.refresh()
		},
		handleSortSuccess(){
			this.getFileMapSortTree();
		}
	}
}
</script>
<style scoped>
.custom-tree-node {
	/*display: flex;*/
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	margin-left: 10px;
	display: none;
}

.custom-tree-node:hover .code {
	display: none;
}

.custom-tree-node:hover .do {
	margin-left: 10px;
	display: inline-block;
}
</style>
